<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of index
 *
 * @author Administrator
 */
class forgot_password extends CI_Controller {

  public function __construct() {
    parent::__construct();
  }

  public function index() {
    $this->load->view('provider/forgot_password', array('captcha' => generate_captcha(225),
        'title_html' => $this->lang->line('label.provider.title.forgot_password')));
  }

  public function process() {
    $errors = array();
    if (!isset($_REQUEST['captcha']) || $_REQUEST['captcha'] != userdata('captcha_session')) {
      $errors['captcha'] = $this->lang->line('error.enter.right.captcha');
    }
    unset_userdata('captcha_session');
    if (empty($_REQUEST['email'])) {
      $errors['email'] = $this->lang->line('error.field.require', $this->lang->line('label.forgot.password.email'));
    } else if (!validate_email($_REQUEST['email'])) {
      $errors['email'] = $this->lang->line('error.field.invalid', array($this->lang->line('label.email'), $_REQUEST['email']));
    }
    if (empty($errors)) {
      $provider = $this->provider_user_model->get(array('email' => $_REQUEST['email']));
      if ($provider) {
        $user = $this->user_model->get($provider['user_id']);
        if ($user) {
          $user['active_code'] = $this->auth->generateActiveCode(50) . uniqid();
          $this->load->library('sendEmail');
          $send = $this->sendemail->sendEmailByTemplate(array_merge($provider, $user, array(
                              'from' => $this->setting_model->item("sender_name"),
                              'link' => site_url('provider/forgot_password/request_password/' . $user['active_code']),
                              'to' => $provider['email'])), 'provider_forgot_password');
          if ($send) {
            $this->user_model->save($user);
            set_flash_message($this->lang->line('message.require.password.success'));
            redirect('provider');
            return;
          } else {
            set_flash_error($this->lang->line('error.system.problem'));
          }
        } else {
          set_flash_error($this->lang->line('error.enter.right.email'));
        }
      } else {
        set_flash_error($this->lang->line('error.enter.right.email'));
      }
    } else {
      set_flash_error($errors);
    }
    $this->load->view('provider/forgot_password', array('captcha' => generate_captcha()));
  }

  public function request_password($active_code = '') {
    $user = $this->user_model->get(array('active_code' => $active_code));
    if (!empty($user)) {
      $provider = $this->provider_user_model->get(array('user_id' => $user['id']));
      if (!empty($provider)) {
        $password = $this->auth->generateActiveCode(10);
        unset($user['password']);
        $this->load->library('sendEmail');
        $send = $this->sendemail->sendEmailByTemplate(array_merge($provider, $user, array(
                            'from' => $this->setting_model->item("sender_name"),
                            'password' => $password, 
                            'to' => $provider['email'])), 'provider_new_password');
        if ($send) {
          $user['password'] = md5($user['salt'] . md5($password));
          $this->user_model->save($user);
          set_flash_message($this->lang->line('message.request.new.password.success'));
        } else
          set_flash_error($this->lang->line('error.system.problem'));
      } else
        set_flash_error($this->lang->line('error.provider.profile.not.found'));
    } else
      set_flash_error($this->lang->line('error.user.not.found'));
    redirect('provider');
  }

}